Sort Numbers in Ascending Order

Theory:

Sorting numbers in ascending order means arranging them from the smallest to the largest.

Python Code:

def sort_numbers(numbers):
    return sorted(numbers)

# Taking input for numbers and sorting them
def sort_and_display_numbers():
    numbers = [int(x) for x in input("Enter numbers separated by spaces: ").split()]
    sorted_numbers = sort_numbers(numbers)
    print("Numbers sorted in ascending order:", sorted_numbers)

sort_and_display_numbers()

Example Output 1:

Enter numbers separated by spaces: 5 2 8 1 4

Numbers sorted in ascending order: [1, 2, 4, 5, 8]

Example Output 2:

Enter numbers separated by spaces: 10 3 7 9 2

Numbers sorted in ascending order: [2, 3, 7, 9, 10]

Code Explanation:

The function sort_numbers(numbers) sorts a list of numbers in ascending order using the built-in sorted() function.

The function sort_and_display_numbers() takes input for numbers, sorts them using the aforementioned function, and prints the sorted numbers.